package cn.zust.edu.cn;

import javax.swing.*;
import java.awt.*;

public class Triangle implements IShape{


    Point ptA;

    Point ptB;

    Point ptC;

    public Triangle(Point ptA, Point ptB, Point ptC) {
        this.ptA = ptA;
        this.ptB = ptB;
        this.ptC = ptC;
    }

    @Override
    public double perimeter() {
        double edge1 = Math.sqrt(Math.pow(ptA.x-ptB.x,2))+Math.sqrt(Math.pow(ptA.y-ptB.y,2));
        double edge2 = Math.sqrt(Math.pow(ptA.x-ptC.x,2))+Math.sqrt(Math.pow(ptA.y-ptC.y,2));
        double edge3 = Math.sqrt(Math.pow(ptB.x-ptC.x,2))+Math.sqrt(Math.pow(ptB.y-ptC.y,2));
        return edge1+edge2+edge3;
    }

    @Override
    public double area() {
        double edge1 = Math.sqrt(Math.pow(ptA.x-ptB.x,2))+Math.sqrt(Math.pow(ptA.y-ptB.y,2));
        double edge2 = Math.sqrt(Math.pow(ptA.x-ptC.x,2))+Math.sqrt(Math.pow(ptA.y-ptC.y,2));
        double edge3 = Math.sqrt(Math.pow(ptB.x-ptC.x,2))+Math.sqrt(Math.pow(ptB.y-ptC.y,2));
        double perimeter = (edge1+edge2+edge3)/2;
        return Math.sqrt(perimeter*(perimeter-edge1)*(perimeter-edge2)*(perimeter-edge3));
    }

    @Override
    public void draw() {
        double perimeter = perimeter();
        double area = area();
        System.out.println("绘制三角形 ——— 周长: " + perimeter + ",面积:" + area);
        double[] xPoints = {ptA.x, ptB.x, ptC.x};
        double[] yPoints = {ptA.y, ptB.y, ptC.y};

        JFrame frame = new JFrame("绘制三角形");
        frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);
        frame.setSize(400, 400);

        JPanel panel = new JPanel(){
            @Override
            protected void paintComponent(Graphics g) {
                super.paintComponent(g);
                g.setColor(Color.BLUE);
                g.drawPolygon(new int[]{(int)xPoints[0], (int)xPoints[1], (int)xPoints[2]},
                        new int[]{(int)yPoints[0], (int)yPoints[1], (int)yPoints[2]}, 3);
            }
        };

        frame.add(panel);
        frame.setVisible(true);

    }
}
